//
//  UIImage+XUtilExtension.m
//  XUtils
//
//  Created by Hongwei Wang on 2018/8/6.
//  Copyright © 2018年 Hongwei Wang. All rights reserved.
//

#import "UIImage+XUtilExtension.h"

@implementation UIImage (XUtilExtension)

+ (UIImage *)defaultHeadImage {
    return [UIImage imageNamed:@"pho_default-avatar_man"];
}

+ (UIImage *)rectanglePlaceHolder {
    return [UIImage imageNamed:@"rectangle_place_holder"];
}

+ (UIImage *)squarePlaceHolder {
    return [UIImage imageNamed:@"square_place_holder"];
}

+ (UIImage *)bookPlaceholder {
    return [UIImage imageNamed:@"book_holder"];
}

+ (UIImage *)noCover {
    return [UIImage imageNamed:@"暂无封面"];
}

+ (UIImage *)imageWithColor:(UIColor *)color {
    if (!color) {
        return [UIImage new];
    }
    CGRect rect=CGRectMake(0,0, 1, 1);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

- (UIImage *)blurryImageWithBlurLevel:(CGFloat)blur {
    CIImage *inputImage = [CIImage imageWithCGImage:self.CGImage];
    CIContext *context = [CIContext contextWithOptions:nil];
    CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur" keysAndValues:kCIInputImageKey, inputImage, @"inputRadius", @(blur), nil];
    CIImage *outputImage = filter.outputImage;
    CGImageRef outImage = [context createCGImage:outputImage
                                        fromRect:[outputImage extent]];
    return [UIImage imageWithCGImage:outImage];
}

- (instancetype)circleImage
{
    // 开启图形上下文 （这个就用到前面的UIView的分类可以直接点出来）
    UIGraphicsBeginImageContext(self.size);

    // 获得上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    // 矩形框
    CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);

    // 添加一个圆
    CGContextAddEllipseInRect(ctx, rect);

    // 裁剪(裁剪成刚才添加的图形形状)
    CGContextClip(ctx);

    // 往圆上面画一张图片
    [self drawInRect:rect];

    // 获得上下文中的片
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    // 关闭图形上下文
    UIGraphicsEndImageContext();

    return image;
}

- (instancetype)resetSize:(CGSize)size {
    CGFloat scale = [[UIScreen mainScreen]scale];
    UIGraphicsBeginImageContextWithOptions(size, NO, scale);
    [self drawInRect:CGRectMake(0,0,size.width,size.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

- (BOOL)lengthIsMinWidth {
    CGSize size = self.size;
    CGFloat width = size.width;
    CGFloat height = size.height;
    return width <= height;
}

- (instancetype)expandWithMultiplier:(CGFloat)mutipilier {
    CGSize size = self.size;
    CGFloat width = size.width;
    CGFloat height = size.height;
    CGSize newSize = CGSizeMake(width*mutipilier, height*mutipilier);
    return [self resetSize:newSize];
}

+ (UIImage *)imageWithCIImage:(CIImage *)ciImage size:(CGFloat)size {
    CGRect extent = CGRectIntegral(ciImage.extent);
    CGFloat scale = MIN(size / CGRectGetWidth(extent), size / CGRectGetHeight(extent));
    
    // 1.创建bitmap
    size_t width = CGRectGetWidth(extent) * scale;
    size_t height = CGRectGetHeight(extent) * scale;
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceCMYK();
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef bitmapImage = [context createCGImage:ciImage fromRect:extent];
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    CGContextScaleCTM(bitmapRef, scale, scale);
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    
    // 2.保存bitmap
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    CGContextRelease(bitmapRef);
    CGImageRelease(bitmapImage);
    return [UIImage imageWithCGImage:scaledImage];
}


@end
